package Java7_Concurrency_Cookbook.concurrent_custom.custom_executor_use_custom_thread_factory;

import java.util.Date;

/**
 * @author lixiangke
 * @date 2017/11/24
 */
public class MyThread extends Thread {
    private Date creationDate;
    private Date startDate;
    private Date finishDate;

    public MyThread(Runnable target, String name) {
        super(target, name);
        setCreationDate();
    }

    @Override
    public void run() {
        setStartDate();
        super.run();
        setFinishDate();
    }

    public void setCreationDate() {
        this.creationDate = new Date();
    }

    public void setStartDate() {
        this.startDate = new Date();
    }

    public void setFinishDate() {
        this.finishDate = new Date();
    }

    public long getExecutionTime() {
        return finishDate.getTime() - startDate.getTime();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getName() + " : Creation Date: " + creationDate + " : Running time : " + getExecutionTime() + " Milliseconds.");
        return sb.toString();
    }
}
